package com.shop.controller;

import com.shop.pojo.DtsCoupon;
import com.shop.service.DtsCouponService;
import com.shop.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName AdminCouponController
 * @Author xiao宋
 * @Date 8:13  2022/10/20
 * @Version 1.0
 * @Description TODO
 */
@Slf4j
@RestController
@RequestMapping("/admin/coupon")
public class AdminCouponController {
    @Autowired
    DtsCouponService couponService;

    /**
     * 显示优惠券列表
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param name
     * @param type
     * @param status
     * @return
     */
    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
            @RequestParam(value = "sort", required = false, defaultValue = "add_time") String sort,
            @RequestParam(value = "order", required = false, defaultValue = "desc") String order,
            String name, Integer type, Integer status
    ) {
        Map<String, Object> data;
        try {
            data = couponService.findByPage(page, limit, sort, order, name, status, type);
        } catch (Exception e) {
            return ResponseUtil.fail(-1, e.getMessage());
        }
        return ResponseUtil.ok(data);
    }

    /**
     * 优惠券详情 --> 显示用户对应优惠券列表
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param couponId 要显示的优惠券 ID
     * @param userId   用户查找时候的用户 ID
     * @param status   用户的使用优惠券状态
     * @return
     */
    @GetMapping("/listuser")
    public Object listuser(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
            @RequestParam(value = "sort", required = false, defaultValue = "add_time") String sort,
            @RequestParam(value = "order", required = false, defaultValue = "desc") String order,
            Integer couponId, Integer userId, Integer status
    ) {
        Map<String, Object> data;
        try {
            data = couponService.findByCouponUserPage(page, limit, sort, order, status, userId, couponId);
        } catch (Exception e) {
            return ResponseUtil.fail(-1, e.getMessage());
        }
        return ResponseUtil.ok(data);
    }

    /**
     * 点优惠券详情会调用此方法
     * @param id
     * @return 然后该优惠券的详细信息
     */
    @GetMapping("/read")
    public Object read(Integer id) {
        DtsCoupon data=couponService.findByCouponId(id);
        return ResponseUtil.ok(data);
    }
}
